﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Cash.BaseClass;
using System.Configuration;
using Huayou.Common;
using PayRoller.Code;
using System.Web.SessionState;
using HySoft.Cash.Business;



namespace PayRoller.Manage.Reports
{
    public partial class ClientReportList : ParentPage, IRequiresSessionState
    {
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }


        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string commandText = "";
            string str2 = "";
            if (!this.txtOurRefID.Text.Trim().Equals(""))
            {
                str2 = str2 + " and Sid=" + this.txtOurRefID.Text.Trim();
            }

            int orgIDSelected = this.UOrgNameInput.OrgIDSelected;
            if (orgIDSelected > 0)
            {
                str2 = str2 + " and Sid=" + orgIDSelected;
            }
            if (!this.txtFromDate.Text.Trim().Equals("") && (this.txtFromDate.Text.Trim() != "dd/mm/yyyy"))
            {
                str2 = str2 + " and RegDate>='" + this.txtFromDate.Text + "'";
            }
            if (!this.txtToDate.Text.Trim().Equals("") && (this.txtToDate.Text.Trim() != "dd/mm/yyyy"))
            {
                str2 = str2 + " and RegDate<='" + this.txtToDate.Text + "'";
            }
            commandText = "select * from tOrgInfo where 1=1  " + str2 + " order by Sid desc";

            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.Text, commandText).Tables[0];
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }



        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }


        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                int currentOrgID = Convert.ToInt32(drv["Sid"]);
                string orgDetailsURl = string.Format("~/manage/OrgInfo/Index.aspx?OrgSid={0}", currentOrgID);
                HyperLink hlOurRefID = e.Item.FindControl("hlOurRefID") as HyperLink;
                if (hlOurRefID != null)
                {
                    hlOurRefID.NavigateUrl = orgDetailsURl;
                    hlOurRefID.Text = currentOrgID.ToString();
                }

                HyperLink hlClientName = e.Item.FindControl("hlClientName") as HyperLink;
                if (hlClientName != null)
                {
                    hlClientName.NavigateUrl = orgDetailsURl;
                    hlClientName.Text = drv["OrgName"].ToString();
                }



                decimal creditLimit = Convert.ToDecimal(drv["CreditLimit"]);
                decimal closingBalance = Convert.ToDecimal(drv["CreditBalance"]); //Convert.ToDecimal(TorginfoBN.GetClosingBalance(currentOrgID));

                Label hlCreditLimit = e.Item.FindControl("hlCreditLimit") as Label;
                if (hlCreditLimit != null)
                {
                    hlCreditLimit.Text = creditLimit.ToString();
                }

                Label hlCreditBalance = e.Item.FindControl("hlCreditBalance") as Label;
                if (hlCreditBalance != null)
                {
                    hlCreditBalance.Text = closingBalance.ToString(); ;
                }

                Label hlAvailableBalance = e.Item.FindControl("hlAvailableBalance") as Label;
                if (hlAvailableBalance != null)
                {
                    hlAvailableBalance.Text = (creditLimit - closingBalance).ToString();
                }

                HyperLink hlEmployees = e.Item.FindControl("hlEmployees") as HyperLink;
                if (hlEmployees != null)
                {
                    using (TempinfoBN obn2 = new TempinfoBN(this.Page))
                    {
                        //需要根据当前企业ID获取其下的员工列表
                        obn2.QueryOrgSid(currentOrgID);
                        hlEmployees.Text = obn2.GetList().Rows.Count.ToString();
                        hlEmployees.NavigateUrl = string.Format("~/manage/OrgInfo/EmpList.aspx?OrgSid={0}", currentOrgID);
                    }
                }

            }
        }
    }
}
